package com.photoeditor.demo.model.subscribe.manager;

import android.app.Activity;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.event.FeedBackEvent;
import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.model.bean.sub.FunctionBean;
import com.photoeditor.demo.model.subscribe.strategy.SVipStrategyManager;
import com.photoeditor.demo.ui.activity.subscribe.SubscribeActivity;
import com.photoeditor.demo.ui.activity.subscribe.SubscribeFunctionsActivity;
import com.photoeditor.demo.ui.activity.subscribe.SubscribeRetainDialogAActivity;
import com.photoeditor.demo.ui.activity.subscribe.SubscribeRetainDialogBActivity;
import com.photoeditor.demo.ui.activity.subscribe.SubscribeVideoActivity;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;

import org.greenrobot.eventbus.EventBus;

import static com.photoeditor.demo.model.bean.face.BaseFaceResultBean.REPORT_TYPE_BABYPREDICT;
import static com.photoeditor.demo.model.bean.face.BaseFaceResultBean.REPORT_TYPE_EXOTIC;
import static com.photoeditor.demo.model.bean.face.BaseFaceResultBean.REPORT_TYPE_FACE_AGING;
import static com.photoeditor.demo.model.bean.face.BaseFaceResultBean.REPORT_TYPE_GENDER_SWITCH;
import static com.photoeditor.demo.model.bean.face.BaseFaceResultBean.REPORT_TYPE_PAST_LIFE;
import static com.photoeditor.demo.model.subscribe.manager.SVipConstant.REQUEST_CODE_FOR_VIP_ACTIVITY;

/**
 * Created by Lai on 2018/10/24 0024.
 */
public class SVipController {

    /**
     * 其他入口调用主订阅页面
     * @param activity
     * @param entrance
     * @return
     */
    public static boolean showSubscribeActivity(Activity activity, @SVipConstant.SVipEntrance int entrance, String entranceStatistic) {
        return showSubscribeActivity(activity, SVipConstant.VIP_ENTRANCE_IN_APP, entranceStatistic, null, REQUEST_CODE_FOR_VIP_ACTIVITY, false);
    }

    /**
     * splash页面专属  区别：finish后会startMainActivity
     * @param activity
     * @param entrance
     * @return
     */
    public static boolean showSubscribeActivityFromSplash(Activity activity, @SVipConstant.SVipEntrance int entrance) {
        return showSubscribeActivity(activity, entrance, StatisticsConstant.ENTRANCE_VALUE_OPEN_APP,null, REQUEST_CODE_FOR_VIP_ACTIVITY, true);
    }

    /**
     *
     * @param activity
     * @param entrance
     * @param listener
     * @param requestCode
     * @param startMain 结束后时候启动主页面
     * @return
     */
    private static boolean showSubscribeActivity(Activity activity, @SVipConstant.SVipEntrance int entrance, String entranceStatistic, SVipController.OnSubscribeCloseListener listener, int requestCode, boolean startMain) {
        if (SVipStrategyManager.getInstance().canShowSVip(entrance)) {
            int pageId = SVipStrategyManager.getInstance().getWitchPage(entrance);
            switch (pageId) {
                case SVipConstant.VIP_ACTIVITY_A:
                    statisticFullScreenSubscribe();
                    SubscribeVideoActivity.startActivity(activity, entranceStatistic, listener, requestCode, startMain);
                    break;
                case SVipConstant.VIP_ACTIVITY_B:
                    statisticFullScreenSubscribe();
                    SubscribeActivity.startActivity(activity, entranceStatistic, listener, requestCode, startMain);
                    break;
                default:
                    statisticFullScreenSubscribe();
                    SubscribeVideoActivity.startActivity(activity, entranceStatistic, listener, requestCode, startMain);
                    break;
            }
            if (SpUtils.obtain().getBoolean("st_n_ms", true)) {
                SpUtils.obtain().save("st_n_ms", false);
                StatisticsUtils.statisticGrowingIo(StatisticsConstant.EVENT_NEW_MAIN_SUB);
            }
            SVipStrategyManager.getInstance().saveShowCountToday(entrance);
            SVipStrategyManager.getInstance().saveLastShowTime(entrance);
            return true;
        }
        return false;
    }

    //根据挽留弹窗策略选择不同的挽留弹窗
    public static boolean showRetainDialog(Activity activity, String entrance, int requestCode) {
        if (SVipStrategyManager.getInstance().canShowRetain()) {
            int abTestType = SVipStrategyManager.getInstance().getWitchPageWithAB(SVipConstant.VIP_ENTRANCE_RETAIN);
                SpUtils.obtain().save("st_n_rt_t", System.currentTimeMillis());
                StatisticsUtils.statisticGrowingIo(StatisticsConstant.EVENT_NEW_RETAIN);
                switch (abTestType) {
                    case SVipConstant.VIP_DIALOG_A:
                        SubscribeRetainDialogAActivity.startActivity(activity, entrance, requestCode, null);
                        break;
                    case SVipConstant.VIP_DIALOG_B:
                        SubscribeRetainDialogBActivity.startActivity(activity, entrance, requestCode, null);
                        break;
                    default:
                        SubscribeRetainDialogAActivity.startActivity(activity, entrance, requestCode, null);
                        break;
                }
                return true;
        }
        return false;
    }


    //老化，换性，前世今生，宝宝功能,滤镜，贴纸等入口调用主订阅页
    public static void showFunctionSubscribe(Activity activity, int funType,String entranceStatistic, OnSubscribeCloseListener subscribeCloseListener) {
        //根据类型选择跳转不同的功能订阅页
        //entranceStatistic代表是滤镜还是贴纸等类型
        if (SVipStrategyManager.getInstance().canShowSVip(SVipConstant.VIP_ENTRANCE_IN_APP)) {
            SubscribeFunctionsActivity.startActivity(activity, getStatisticEntranceByFunType(funType), entranceStatistic,subscribeCloseListener, 99, false);
        }
    }


    private static String getStatisticEntranceByFunType(int funType) {
        String ret = "";
        switch (funType) {
            case REPORT_TYPE_FACE_AGING:
                ret = StatisticsConstant.ENTRANCE_VALUE_FACE_AGING;
                break;
            case REPORT_TYPE_EXOTIC:
                ret = StatisticsConstant.ENTRANCE_VALUE_FACE_SWAP;
                break;
            case REPORT_TYPE_GENDER_SWITCH:
                ret = StatisticsConstant.ENTRANCE_VALUE_GENDER_SWAP;
                break;
            case REPORT_TYPE_PAST_LIFE:
                ret = StatisticsConstant.ENTRANCE_VALUE_PAST_LIFE;
                break;
            case REPORT_TYPE_BABYPREDICT:
                ret = StatisticsConstant.ENTRANCE_VALUE_BABY_PREDICT;
                break;
            default:
                break;
        }
        return ret;
    }

    private static FunctionBean getFunctionBean(int funType, String statisticEntranceNew, boolean first) {
        FunctionBean functionBean = null;
        String alias = SVipConstant.getAliasByFunType(funType);
        int hideVideo = 1;
        int witchPage = SVipStrategyManager.getInstance().getWitchPage(SVipConstant.VIP_ENTRANCE_ADVANCE_FEATURE);
        if (witchPage == SVipConstant.VIP_FUTURE_A) {
            //默认B样式，隐藏视频
            hideVideo = 1;
        }
        switch (funType) {
            case REPORT_TYPE_FACE_AGING:

                functionBean = new FunctionBean(REPORT_TYPE_FACE_AGING, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_GETOLDPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_GETOLDPAGEB,
                        -1, -1,
                        R.string.vip_feature_age_title,
                        R.string.vip_feature_age_conten1,
                        R.string.vip_feature_age_conten2,
                        500, 8320, 14800, hideVideo);
                break;
            /*case BaseFaceResultBean.REPORT_TYPE_BABYPREDICT:

                functionBean = new FunctionBean(BaseFaceResultBean.REPORT_TYPE_BABYPREDICT, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_BABYPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_BABYPAGEB,
                        R.drawable.palm_baby_first_frame, R.raw.palm,
                        R.string.vip_feature_baby_title,
                        R.string.txt_feature_baby,
                        R.string.vip_feature_baby_conten2,
                        200,16200,19440, hideVideo);
                break;*/
            /*case BaseFaceResultBean.REPORT_TYPE_BEAUTYCONTEST:

                functionBean = new FunctionBean(SkuManager.TYPE_BEAUTY_CONTEST, statisticEntranceNew,
                        StatisticsConstant.STATISTIC_EVENT_VIP_VALUE_PAGE_DIALOG_BEAUTY_CONTEST_NEW,
                        R.drawable.dialog_banner_beauty, getString(R.string.vip_dialog_beauty_contest_title),
                        getString(R.string.vip_dialog_beauty_contest_content), getString(R.string.vip_dialog_beauty_contest_content_2));
                break;*/
            /*case BaseFaceResultBean.REPORT_TYPE_ETHNICITY:

                functionBean = new FunctionBean(BaseFaceResultBean.REPORT_TYPE_ETHNICITY, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_ETHNICITYPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_ETHNICITYPAGEB,
                        R.drawable.ethnicity_first_frame, R.raw.ethnicity_ad,
                        R.string.vip_feature_ethnicity_title_b,
                        R.string.txt_ethnic,
                        R.string.vip_feature_ethnicity_conten2,
                        1000,-1,-1, hideVideo);
                break;*/
            /*case BaseFaceResultBean.REPORT_TYPE_FUTUREPREDICTOR:

                functionBean = new FunctionBean(SkuManager.TYPE_FUTURE_PREDICTOR, alias, statisticEntranceNew,
                        hideVideo > 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_ETHNICITYPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_ETHNICITYPAGEB,
                        R.drawable.ic_vip_video_future, R.raw.palm, R.string.vip_feature_b_future, 500);
                break;*/
            /*case BaseFaceResultBean.REPORT_TYPE_PALM:

                functionBean = new FunctionBean(BaseFaceResultBean.REPORT_TYPE_PALM, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_HANDPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_HANDPAGEB,
                        R.drawable.palm_hand_first_frame, R.raw.palm,
                        R.string.vip_feature_palm_title_c,
                        R.string.txt_palm_scanner,
                        R.string.vip_feature_age_conten2,
                        500,520,7160, hideVideo);
                break;*/
            /*case BaseFaceResultBean.REPORT_TYPE_HOROSCOPE:

                functionBean = new FunctionBean(SkuManager.TYPE_YEARLY, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_ETHNICITYPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_ETHNICITYPAGEB,
                        R.drawable.ic_vip_video_future, R.raw.palm,
                        R.string.vip_feature_horoscope_title_b,
                        R.string.vip_feature_horoscope_conten1,
                        R.string.vip_feature_horoscope_conten2,
                        1000,550,1000);
                break;*/
            case REPORT_TYPE_EXOTIC:

                functionBean = new FunctionBean(REPORT_TYPE_EXOTIC, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_FACE_SWAP_PAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_FACE_SWAP_PAGEB,
                        -1, -1,
                        R.string.vip_feature_exotic_title,
                        R.string.vip_feature_exotic_conten1,
                        R.string.vip_feature_exotic_conten2,
                        200, -1, -1, hideVideo);
                break;

            case REPORT_TYPE_GENDER_SWITCH:
                functionBean = new FunctionBean(REPORT_TYPE_GENDER_SWITCH, alias, statisticEntranceNew,
                        hideVideo < 0 ? StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_GENDERPAGEA : StatisticsConstant.STATISTIC_EVENT_SUBSCRIBE_GENDERPAGEB,
                        -1, -1,
                        R.string.vip_feature_gender_switch_title,
                        R.string.vip_feature_gender_switch_conten1,
                        R.string.vip_feature_gender_switch_conten2,
                        200, -1, -1, hideVideo);

                break;
            default:
                break;
        }
        return functionBean;
    }

    public static String getString(int id) {
        return ComponentContext.getContext().getResources().getString(id);
    }

    public interface OnSubscribeCloseListener {
        void onSubscribeClose();
    }

    private static void statisticFullScreenSubscribe() {

    }
}
